﻿using CPAIS.Adapter;
using CPAIS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CPAIS.Web
{
    /// <summary>
    /// Represents a memory log model manager.
    /// </summary>
    public class MLManager
    {
        private class Item
        {
            private LogModel model;
            private LDAGibbsAdapter adapterForLDA;
            private CPMGibbsAdapter adapterForCPM;
            private HLDAGibbsAdapter adapterForHLDA;
            private HDPGibbsAdapter adapterForHDP;
            private TestCPMAnalysisSpeedAdapter adapterForTAS;

            public Item(LogModel model)
            {
                this.model = model;
                adapterForLDA = new LDAGibbsAdapter(model);
                adapterForCPM = new CPMGibbsAdapter(model);
                adapterForHLDA = new HLDAGibbsAdapter(model);
                adapterForHDP = new HDPGibbsAdapter(model);
                adapterForTAS = new TestCPMAnalysisSpeedAdapter(model);
            }

            public LogModel Model { get { return model; } }
            public LDAGibbsAdapter AdapterForLDA { get { return adapterForLDA; } }
            public CPMGibbsAdapter AdapterForCPM { get { return adapterForCPM; } }
            public HLDAGibbsAdapter AdapterForHLDA { get { return adapterForHLDA; } }
            public HDPGibbsAdapter AdapterForHDP { get { return adapterForHDP; } }
            public TestCPMAnalysisSpeedAdapter AdapterForTAS { get { return adapterForTAS; } }
        }
                
        private Dictionary<string, Item> items;
        public MLManager()
        { 
            items = new Dictionary<string, Item>();
        }
        
        public void AddLogModel(LogModel model)
        {
            //int hashCode = model.LogName.GetHashCode();
            if (!items.ContainsKey(model.LogName))
                items.Add(model.LogName, new Item(model));
        }

        public void RemoveLogModel(string logName)
        {
            items.Remove(logName);
        }

        public void RemoveAllModels()
        {
            items.Clear();
        }

        public LogModel GetLogModel(string logName)
        {
            //int hashCode = logName.GetHashCode();
            if (items.ContainsKey(logName))
                return items[logName].Model;
            return null;
        }

        public LDAGibbsAdapter GetLDAAdapter(string logName)
        {
            //int hashCode = logName.GetHashCode();
            if (items.ContainsKey(logName))
                return items[logName].AdapterForLDA;
            return null;
        }

        public CPMGibbsAdapter GetCPMAdapter(string logName)
        {
            //int hashCode = logName.GetHashCode();
            if (items.ContainsKey(logName))
                return items[logName].AdapterForCPM;
            return null;
        }

        public HLDAGibbsAdapter GetHLDAAdapter(string logName)
        {
            //int hashCode = logName.GetHashCode();
            if (items.ContainsKey(logName))
                return items[logName].AdapterForHLDA;
            return null;
        }

        public HDPGibbsAdapter GetHDPAdapter(string logName)
        {
            //int hashCode = logName.GetHashCode();
            if (items.ContainsKey(logName))
                return items[logName].AdapterForHDP;
            return null;
        }

        public TestCPMAnalysisSpeedAdapter GetTASAdapter(string logName)
        {
            //int hashCode = logName.GetHashCode();
            if (items.ContainsKey(logName))
                return items[logName].AdapterForTAS;
            return null;
        }
    }
}